<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: sap<songaiping@hrbjjhh.com>
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * @todo 客服系统-管理
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2014-9-3
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;

class ServiceManageController extends AdminController {

	/*---------------------------- 活动管理 -----------------------------*/
	
	/**
	 * @todo 活动列表
	 * @author sap
	 */
	public function index(){
		$where = array();//初始化查询条件
		$where['status']=array('gt',-1);
		$list = $this->lists('ServiceActivity',$where,'activity_id desc','activity_id,activity_name,start_time,end_time,status');
		int_to_string($list,array('status'=>array(-1=>'删除',0=>'未推荐',1=>'推荐中',2=>'已结束',3=>'回访中',4=>'回访任务结束')));
		$this->assign('_list',$list);
		$this->assign('meta_title','活动列表');
		$this->display('activity_list');
	}
	
	/**
	 * @todo 拉取活动
	 * @author sap
	 */
	public function addActivity(){
		$obj = D('Admin/ServiceActivity');
		if(!$obj->activityAdd()){//拉取活动数据
			$this->error($obj->getError());
		}
		//记录行为
		action_log('activity_get', 'service_activity', UID, UID);
		$this->success('拉取成功');
	}
	
	/**
	 * @todo 活动设置
	 * @author sap
	 * @since 2015-9-3
	 */
	public function activitySet(){
		//获取数据
		$activity_id = (int)I('activity_id');
		if($activity_id == 0){
			$this->error('请正确操作');
		}
		//设置页面数据
		$info = D('Admin/ServiceActivity')->activityShow($activity_id,'activity_id,activity_name,start_time,end_time');//活动详情
		if(empty($info)){
			$this->error('请正确操作');
		}
		$obj = D('Admin/ServiceApi');
		$bus_list = $obj->getActivityBusList($activity_id);//班车
		$departure_time_list = $obj->getActivityDepartureTime($activity_id);//发车时间
		$sale_point_list = $obj->getActivitySalePoint($activity_id);//销售网点
		$sms_list = $obj->getActivitySmsTemplate($activity_id);//短信模板
		
		//显示
		$this->assign('list',$info);
		$this->assign('bus_list',$bus_list);
		$this->assign('departure_time_list',$departure_time_list);
		$this->assign('sale_point_list',$sale_point_list);
		$this->assign('sms_list',$sms_list);
		$this->assign('meta_title_c','活动列表');
		$this->assign('meta_title','活动设置');
		$this->display('activity_set');
						
	}
	
	/**
	 * @todo 添加活动下销售网点
	 * @author sap
	 */
	public function addActivitySalePoint(){
		$activity_id = (int)I('activity_id');//活动ID判断
		if($activity_id == 0){
			$this->error('请正确操作');
		}
		if(IS_POST){
			//获取数据及判断
			$sale_point_id = array_filter(array_unique((array)I('sale_point_id',0)));
			if(empty($sale_point_id)){
				$this->error('请选择销售网点');
			}
			//操作
			$obj = D('Admin/ServiceActivity');
			if(!$obj->addActivityRelation($activity_id,$sale_point_id,'sale_point_id','service_activity_sale_point','service_activity_sale_point_list')){
				$this->error($obj->getError());
			}
			//记录行为
			action_log('activity_sale_point_add', 'service_activity_sale_point', $activity_id, UID);
			$this->success('操作成功',U('ServiceManage/activitySet',array('activity_id'=>$activity_id)));
		}else{
			$this->assign('activity_id',$activity_id);
			$this->assign('meta_title_c','活动设置');
			$this->assign('meta_title','添加活动下销售网点');
			$this->display('activity_sale_point_add');
		}
	}
	
	/**
	 * @todo 添加活动下班车点
	 * @author sap
	 */
	public function addActivityBus(){
		$activity_id = (int)I('activity_id');//活动ID判断
		if($activity_id == 0){
			$this->error('请正确操作');
		}
		if(IS_POST){
			//获取数据及判断
			$bus_point_id = array_filter(array_unique((array)I('bus_point_id',0)));
			if(empty($bus_point_id)){
				$this->error('请选择班车点');
			}
			//操作
			$obj = D('Admin/ServiceActivity');
			if(!$obj->addActivityRelation($activity_id,$bus_point_id,'bus_point_id','service_activity_bus','service_activity_bus_list')){
				$this->error($obj->getError());
			}
			//记录行为
			action_log('activity_bus_add', 'service_activity_bus', $activity_id, UID);
			$this->success('操作成功',U('ServiceManage/activitySet',array('activity_id'=>$activity_id)));
		}else{
			$this->assign('activity_id',$activity_id);
			$this->assign('meta_title_c','活动设置');
			$this->assign('meta_title','添加活动下班车点');
			$this->display('activity_bus_add');
		}
	}
	
	/**
	 * @todo 添加活动下短信模板
	 * @author sap
	 */
	public function addActivitySms(){
		$activity_id = (int)I('activity_id');//活动ID判断
		if($activity_id == 0){
			$this->error('请正确操作');
		}
		if(IS_POST){
			//获取数据及判断
			$sms_template_id = array_filter(array_unique((array)I('mb_tpl',0)));
			if(empty($sms_template_id)){
				$this->error('请选择短信模板');
			}
			//操作
			if(!D('Admin/ServiceActivity')->addActivityRelation($activity_id,$sms_template_id,'sms_template_id','service_activity_sms','service_activity_sms_list')){
				$this->error(D('Admin/ServiceActivity')->getError());
			}
			//记录行为
			action_log('activity_sms_add', 'service_activity_sms', $activity_id, UID);
			$this->success('操作成功',U('ServiceManage/activitySet',array('activity_id'=>$activity_id)));
		}else{
			$obj =D('Admin/ServiceActivity');
			$result = $obj->notIsActivitySms($activity_id);//读取所有不在该活动下的短信模板
			if($result === false){
				$this->error($obj->getError());
			}
			$this->assign('_list',$result);
			$this->assign('activity_id',$activity_id);
			$this->assign('meta_title_c','活动设置');
			$this->assign('meta_title','添加活动下短信模板');
			$this->display('activity_sms_add');
		}
	}
	
	/**
	 * @todo 添加活动下发车时间
	 * @author sap
	 */
	public function addActivityDepartureTime(){
		$activity_id = (int)I('activity_id');//活动ID判断
		if($activity_id == 0){
			$this->error('请正确操作');
		}
		if(IS_POST){
			//获取数据及判断
			$departure_time = strtotime(I('departure_time'));
			if($departure_time == 0){
				$this->error('请输入时间');
			}
			//操作
			$obj = D('Admin/ServiceActivity');
			if(!$obj->addActivityDepartureTime($activity_id,$departure_time)){
				$this->error($obj->getError());
			}
			//记录行为
			action_log('activity_departure_time_add', 'service_activity_departure_time', $activity_id, UID);
			$this->success('操作成功',U('ServiceManage/activitySet',array('activity_id'=>$activity_id)));
		}else{
			$this->assign('activity_id',$activity_id);
			$this->assign('meta_title_c','活动设置');
			$this->assign('meta_title','添加活动下发车时间');
			$this->display('activity_departure_time_add');
		}
	}
	
	/**
	 * @todo 活动状态修改
	 * @param string $method 标识
	 * @author sap
	 */
	public function changeStatus(){
		//获取数据
		$status = (int)I('status');
		$activity_id = (int)I('activity_id',0);
		if($activity_id == 0 || $status == 0) {
			$this->error('请选择要操作的数据!');
		}
		//操作数据
		$obj = D('Admin/ServiceActivity');
		if(!$obj->changeStatus($activity_id,$status)){
			$this->error($obj->getError());
		}
		$this->success('操作成功');
	}
	
	/**
	 * @todo 从活动中取报名数据
	 * @author sap
	 * @since 2015-9-4
	 */
	public function addActivityJoin(){
		$activity_id = (int)I('activity_id');
		if($activity_id == 0){
			$this->error('请正确操作');
		}
		$obj = D('Admin/ServiceActivityJoin');
		$result = $obj->addAllActivityJoin($activity_id);
		if($result === false){
			$this->error($obj->getError());
		}
		//记录行为
		action_log('activity_join_add', 'service_activity_join', $activity_id, UID);
		$this->success('拉取报名成功,还可摘取'.$result.'次');
	}
	
	/**
	 * @todo 分配未绑定客户
	 * @author sap
	 */
	public function setBindMember(){
		$activity_id = (int)I('activity_id');
		if($activity_id == 0){
			$this->error('请正确操作');
		}
		if(IS_POST){
			$sign_channel = (int)I('sign_channel');
			$assign_num = (array)I('assign_num',0);
			$member_id = array_filter(array_unique((array)I('member_id',0)));
			if(empty($member_id) || empty($assign_num)){
				$this->error('请正确操作');
			}
			$obj = D('ServiceActivityJoin');
			if(!$obj->setBindMember($activity_id,$sign_channel,$assign_num,$member_id)){
				$this->error($obj->getError());
			}
			//记录行为
			action_log('set_bind_member', 'service_my_customer', UID, UID);
			$this->success('操作成功',U('ServiceManage/index'));
		}else{
			$list_num = D('ServiceActivityJoin')->notBindCustomerNum($activity_id);//按渠道读待分配客户数
			if(empty($list_num)){
				$this->error('无待分配客户');
			}
			$this->assign('list_num',$list_num);
			$this->assign('meta_title_c','活动列表');
			$this->assign('meta_title','分配未绑定客户');
			$this->display('set_bind_member');
		}
	}
	
	/**
	 * @todo 按座席回收绑定人 - 选择回收页面
	 * @author sap
	 */
	public function changeBindMember(){
		$this->assign('meta_title_c','活动列表');
		$this->assign('meta_title','按座席回收');
		$this->display('change_bind_member');
	}
	
	/**
	 * @todo 按座席回收绑定人 - 选择分配页面
	 * @author sap
	 */
	public function changeBindMemberNew(){
		if(!IS_POST){
			$this->error('请正确操作');
		}
		//获取数据
		$member_id = array_filter(array_unique((array)I('member_id',0)));
		if(empty($member_id)){
			$this->error('参数不正确');
		}
		$count_num = M('ServiceMyCustomer')->where(array('member_id'=>array('in',$member_id)))->count();
		if($count_num == 0){
			$this->error('无待分配名额');
		}
		$this->assign('member_id',$member_id);
		$this->assign('count_num',$count_num);
		$this->assign('meta_title_c','活动列表');
		$this->assign('meta_title','按座席回收');
		$this->display('change_bind_member_save');
	}
	
	/**
	 * @todo 按座席回收绑定人 - 执行操作
	 * @author sap
	 */
	public function changeBindMemberSave(){
		if(!IS_POST){
			$this->error('请正确操作');
		}
		$assign_num = (array)I('assign_num',0);
		$member_id = array_filter(array_unique((array)I('member_id',0)));
		$old_member_id = array_filter(array_unique((array)I('old_member_id',0)));
		if(empty($member_id) || empty($old_member_id) || empty($assign_num)){
			$this->error('请正确操作');
		}
		$obj = D('ServiceActivityJoin');
		if(!$obj->changeBindMember($assign_num,$member_id,$old_member_id)){
			$this->error($obj->getError());
		}
		//记录行为
		action_log('change_bind_member', 'service_my_customer', UID, UID);
		$this->success('操作成功',U('ServiceManage/changeBindMember'));
	}
	
	/**
	 * @todo 按手机号回收 - 选择回收页面
	 * @author sap
	 */
	public function changeBindCustomer(){
		$search_text = strip_tags(I('search_text'));//搜索内容
		if($search_text != '' && $search_text != null){
			$obj = D('ServiceActivityJoin');
			$list = $obj->getPhoneBing($search_text);
			if($list === false){
				$this->error($obj->getError());
			}
		}else{
			$list = null;
		}
		//返回值
		$this->assign('_list',$list);
		$this->assign('meta_title_c','活动列表');
		$this->assign('meta_title','按客户回收');
		$this->display('change_bind_customer');
	}
	
	/**
	 * @todo 按手机号回收 - 选择绑定页面
	 * @author sap
	 */
	public function changeBindCustomerNew(){
		$customer_id = array_filter(array_unique((array)I('id',0)));
		if(empty($customer_id)){
			$this->error('参数不正确');
		}
		$this->assign('customer_id',$customer_id);
		$this->assign('count_num',count($customer_id));
		$this->assign('meta_title_c','活动列表');
		$this->assign('meta_title','按客户回收');
		$this->display('change_bind_customer_save');
	}
	
	/**
	 * @todo 按手机号回收 - 执行操作
	 * @author sap
	 */
	public function changeBindCustomerSave(){
		if(!IS_POST){
			$this->error('请正确操作');
		}
		$assign_num = (array)I('assign_num',0);
		$member_id = array_filter(array_unique((array)I('member_id',0)));
		$customer_id = array_filter(array_unique((array)I('customer_id',0)));
		if(empty($member_id) || empty($customer_id)){
			$this->error('请正确操作');
		}
		$obj = D('ServiceActivityJoin');
		if(!$obj->changeBindCustomer($assign_num,$member_id,$customer_id)){
			$this->error($obj->getError());
		}
		//记录行为
		action_log('change_bind_customer', 'service_my_customer', UID, UID);
		$this->success('操作成功',U('ServiceManage/changeBindCustomer'));
	}
	
	/**
	 * @todo 按座席回收VIP绑定人 - 选择回收页面
	 * @author sap
	 */
	public function changeVipBindMember(){
	    $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');//删除和未推荐的活动除外的所有活动
	    $this->assign('activity_list',$activity_list);
	    $this->assign('meta_title_c','活动列表');
	    $this->assign('meta_title','按座席回收VIP');
	    $this->display('change_vip_bind_member');
	}
	
	/**
	 * @todo 按座席回收VIP绑定人 - 选择分配页面
	 * @author sap
	 */
	public function changeVipBindMemberNew(){
	    if(!IS_POST){
	        $this->error('请正确操作',U('ServiceManage/changeVipBindMember'));
	    }
	    $activity_id = (int)I('activity_id');
	    if($activity_id == 0){
	        $this->error('请选择活动',U('ServiceManage/changeVipBindMember'));
	    }
	    //获取数据
	    $member_id = array_filter(array_unique((array)I('member_id',0)));
	    if(empty($member_id)){
	        $this->error('参数不正确',U('ServiceManage/changeVipBindMember'));
	    }
	    $map = array();
	    $map['member_id'] = array('in',$member_id);
	    $map['activity_id'] = $activity_id;
	    $map['ticket_level'] = 1;
	    $count_num = D('ServiceActivityJoinView')->where($map)->count();
	    if($count_num == 0){
	        $this->error('无待分配VIP名额',U('ServiceManage/changeVipBindMember'));
	    }
	    $this->assign('activity_id',$activity_id);
	    $this->assign('member_id',$member_id);
	    $this->assign('count_num',$count_num);
	    $this->assign('meta_title_c','活动列表');
	    $this->assign('meta_title','按座席回收VIP');
	    $this->display('change_vip_bind_member_save');
	}
	
	/**
	 * @todo   按活动下成单自然客户回收（自然流量） - 选择回收页面
	 * @author fang
	 */
	public function changeOrderMember(){
	    $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');//删除和未推荐的活动除外的所有活动
	    $this->assign('activity_list',$activity_list);
	    $this->assign('meta_title','成单自然客户回收');
	    $this->display('change_order_member');
	}

	/**
	 * @todo   按活动下成单自然客户回收（自然流量）  － 分配页面
	 * @author fang
	 */
	public function changeOrderMemberNew(){
	    if(!IS_POST){
	        $this->error('请正确操作',U('ServiceManage/changeOrderMember'));
	    }
	    $activity_id = (int)I('activity_id');
	    if($activity_id == 0){
	        $this->error('请选择活动',U('ServiceManage/changeOrderMember'));
	    }
	    // 获取数据
	    $category_id = array_filter(array_unique((array)I('conf_category_id',0)));
	    if(empty($category_id)){
	        $this->error('参数不正确',U('ServiceManage/changeOrderMember'));
	    }
	    
	    $map = array();
	    $map['conf_category_id'] = array('in', $category_id);
	    $map['status'] = 1;
	    $shop_ids = M('shop')->where($map)->getField('shop_id', true);
	    $where['activity_id'] = $activity_id;
	    $where['order_status'] = array('in', '2,4,5,7');
	    if($shop_ids){
	        $where['shop_id'] = array('in', $shop_ids);
	        $customer_ids = M('order')->where($where)->getField('customer_id', true);
	        $g_where = array(
	            'activity_id' => $activity_id,
	            'customer_id' => array('in', array_unique($customer_ids)),
	            'generalize_member_id' => 0,
	        );
	        $customer_id = M('service_activity_join')->where($g_where)->getField('customer_id', true);
	        $count_num = count($customer_id);
	    }else{
	        $count_num = 0;
	    }
	    if($count_num == 0){
	        $this->error('无待分配名额',U('ServiceManage/changeOrderMember'));
	    }
	    $this->assign('activity_id', $activity_id);
	    $this->assign('count_num', $count_num);
	    $this->assign('customer_id', $customer_id);
	    $this->assign('meta_title_c','活动列表');
	    $this->assign('meta_title','按座席回收VIP');
	    $this->display('change_order_member_save');
	}
	
	/**
	 * @todo   按活动下成单自然客户回收（自然流量） - 执行操作
	 * @author fang
	 */
	public function changeOrderMemberSave(){
	    if(!IS_POST){
	        $this->error('请正确操作');
	    }
	    $assign_num = (array)I('assign_num',0);
	    $member_id = array_filter(array_unique((array)I('member_id',0)));
		$customer_id = array_filter(array_unique((array)I('customer_id',0)));
	    $activity_id = (int)I('activity_id');
	    if($activity_id == 0 || empty($member_id) || empty($assign_num)){
	        $this->error('请正确操作');
	    }
		if(empty($member_id) || empty($customer_id)){
			$this->error('请正确操作');
		}
		$obj = D('ServiceActivityJoin');
		if(!$obj->changeBindCustomer($assign_num,$member_id,$customer_id)){
			$this->error($obj->getError());
		}
	    //记录行为
	    action_log('change_order_member', 'service_my_customer', UID, UID);
	    $this->success('操作成功',U('ServiceManage/changeOrderMember'));
	}
	
	
	
	/**
	 * @todo 按座席回收VIP绑定人 - 执行操作
	 * @author sap
	 */
	public function changeVipBindMemberSave(){
	    if(!IS_POST){
	        $this->error('请正确操作');
	    }
	    $assign_num = (array)I('assign_num',0);
	    $member_id = array_filter(array_unique((array)I('member_id',0)));
	    $old_member_id = array_filter(array_unique((array)I('old_member_id',0)));
	    $activity_id = (int)I('activity_id');
	    if($activity_id == 0 || empty($member_id) || empty($old_member_id) || empty($assign_num)){
	        $this->error('请正确操作');
	    }
	    $obj = D('ServiceActivityJoin');
	    if(!$obj->changeVipBindMember($activity_id,$assign_num,$member_id,$old_member_id)){
	        $this->error($obj->getError());
	    }
	    //记录行为
	    action_log('change_vip_bind_member', 'service_my_customer', UID, UID);
	    $this->success('操作成功',U('ServiceManage/changeVipBindMember'));
	}
	
	/*---------------------------- 名单管理 -----------------------------*/
	
	/**
	 * @todo 名单管理
	 * @author sap
	 */
	public function rollCallList(){
		$search_text = strip_tags(I('search_text'));//搜索内容
		$map = array();//初始化查询条件
		//查询条件
		if($search_text != '' && $search_text != null){
			$map['roll_call_name'] = array('like', '%'.(string)$search_text.'%');
		}
		//列表
		$map['status']=array('gt',-1);
		$list = $this->lists('ServiceRollCall',$map,'roll_call_id desc','roll_call_id,roll_call_name,roster_category_id,total_num,create_user_id,create_time,status');
		int_to_string($list,array('status'=>array(-1=>'删除',0=>'空闲中',1=>'抢占中')));
		
		$roster_category_list = D('ServiceManage')->rosterCategoryList(array('status'=>array('gt',-1)),'roster_category_id,roster_category_name');//名单类别
		$this->assign('roster_category_list',$roster_category_list);
		$this->assign('_list',$list);
		$this->assign('meta_title','名单管理');
		$this->display('roll_call_list');
	}
	
	/**
	 * @todo 查看名单详情
	 * @author sap
	 */
	public function infoRollCall(){
		$roll_call_id = (int)I('roll_call_id');
		if($roll_call_id == 0){
			$this->error('请正确访问');
		}
		$info = D('Admin/ServiceManage')->infoRollCall($roll_call_id);
		$this->assign('list',$info);
		$this->assign('meta_title_c','名单管理');
		$this->assign('meta_title','查看名单详情');
		$this->display('roll_call_info');
	}
	
	/**
	 * @todo 名单状态修改
	 * @param string $method 标识
	 * @author sap
	 */
	public function changeRollCallStatus($method=null){
		$roll_call_id = (int)I('id',0);
		if($roll_call_id == 0) {
			$this->error('请选择要操作的数据!');
		}
		$obj = D('ServiceManage');
		$roll_call_show = $obj->rollCallShow($roll_call_id,'status');//读取原状态值，作判断用
		$map['roll_call_id'] = $roll_call_id;
		switch ( strtolower($method) ){
			case 'forbiduser':
				if($roll_call_show['status'] != 1){
					$this->error('请正确操作1');
				}
				$this->forbid('ServiceRollCall', $map,array( 'success'=>'操作成功！', 'error'=>'操作失败！') );
				S('service_roll_call_list',null);
				//记录行为
				action_log('roll_call_status_zero', 'service_roll_call', $roll_call_id, UID);
				break;
			case 'resumeuser':
				if($roll_call_show['status'] != 0){
					$this->error('请正确操作2');
				}
				$obj->changeRollCallStatus($roll_call_id);//生成数+1
				$this->resume('ServiceRollCall', $map,array( 'success'=>'操作成功！', 'error'=>'操作失败！') );
				S('service_roll_call_list',null);
				//记录行为
				action_log('roll_call_status_one', 'service_roll_call', $roll_call_id, UID);
				break;
			case 'deleteuser':
				if($roll_call_show['status'] != 0){
					$this->error('请正确操作3');
				}
				$this->delete('ServiceRollCall', $map );
				//记录行为
				action_log('roll_call_status_del', 'service_roll_call', $roll_call_id, UID);
				S('service_roll_call_list',null);
				S('service_member_roll_call_list',null);
				break;
			default:
				$this->error('参数非法');
		}
	}

	/**
	 * @todo 配置可抢占人员
	 * @author sap
	 */
	public function setRollCallMember(){
		if(IS_POST){
			//获取判断
			$roll_call_id = (array)I('roll_call_id',0);
			$member_id = (array)I('member_id',0);
			//操作
			if(!D('ServiceManage')->setRollCallMember($roll_call_id,$member_id)){
				$this->error(D('ServiceManage')->getError());
			}
			//记录行为
			action_log('roll_call_member_set', 'service_roll_call_member', UID, UID);
			$this->success('操作成功',U('ServiceManage/rollCallList'));
		}else{
			//获取判断
			$roll_call_id = array_unique(array_filter((array)I('id',0)));
			if(empty($roll_call_id) ){
				$this->error('请选择名单');
				return false;
			}
			
			//读取数据
			$obj = D('ServiceManage');
			$count_num = $obj->rollCallCustomer($roll_call_id);//名单下未绑定有效且未持有客户总数
			if(empty($count_num)){
				$this->error('名单下无未绑定有效且未持有客户');
			}
			
			$member_list = $obj->rollCallMemberList($roll_call_id);//原名单可抢占座席人员
			$member_value = array_unique(array_column($member_list, 'member_id'));
			if(!empty($member_value) && count($roll_call_id)>1){
				$this->error('修改时只能修改一个名单');
			}
			$member_value = implode(',',$member_value);
			//返回显示
			$this->assign('roll_call_id',$roll_call_id);
			$this->assign('roll_call_num',count($roll_call_id));
			$this->assign('count_num',$count_num);
			$this->assign('member_value',$member_value);
			
			$this->assign('meta_title_c','名单管理');
			$this->assign('meta_title','配置可抢占人员');
			$this->display('set_roll_call_member');
		}
	}
	
	/**
	 * @todo 清空坐席持有资源
	 * @author sap
	 */
	public function delRollCallCustomerMember(){
		if(IS_POST){
			$member_id = array_unique((array)I('member_id',0));
			$obj = D('ServiceManage');
			if(!$obj->delRollCallCustomerMember($member_id)){
				$this->error($obj->getError());
			}
			//记录行为
			action_log('roll_call_customer_member_del', 'service_roll_call_customer', UID, UID);
			$this->success('操作成功',U('ServiceManage/delRollCallCustomerMember'));
		}else{
			$this->assign('meta_title','清空坐席持有资源');
			$this->display('roll_call_customer_member_del');
		}
	}
	
	/**
	 * @todo 导入名单
	 * @author sap
	 */
	public function addRollCall(){
		if(IS_POST){
			//导入
			$obj = D('ServiceManage');
			$result = $obj->addRollCall();		
			//返回值
			if($result === false){
				$json_data['status'] = 0;
				$json_data['info'] = $obj->getError();
				$this->ajaxReturn($json_data);
			}else{
				$json_data['status'] = 1;
				$json_data['data'] = $result;
				$this->ajaxReturn($json_data);
			}
		}else{
			$obj = D('ServiceManage');
			$roster_category_list = $obj->rosterCategoryList(array('status'=>array('gt',-1)),'roster_category_id,roster_category_name');//名单类别
			$roster_category_show = $obj->rosterCategoryShow(array('roster_category_name'=>'小区'),'roster_category_id');//小区类别信息
			$this->assign('tempurl',C('DOWNLOAD_UPLOAD.rootPath').'File/down.csv');
			$this->assign('roster_category_list',$roster_category_list);
			$this->assign('roster_category_id',$roster_category_show['roster_category_id']);
			$this->assign('meta_title_c','名单管理');
			$this->assign('meta_title','导入名单');
			$this->display('roll_call_add');
		}
	}

	/**
	 * @todo 根据省，市，区读取除活动已经选销售网点外所有销售网点列表（公共方法）不受权限控制-----------(待改)
	 * @author sap
	 */
	public function getSalePointList(){
		//获取数据及判断
		$province_id = (int)I('province');
		$city_id = (int)I('city');
		$ward_id = (int)I('ward');
		$activity_id = (int)I('activity_id');
		$data = array();
		if($activity_id == 0){
			$data['status'] = 0;
			$data['info'] = '参数不正确';
			$this->ajaxReturn($data);
		}
		if($province_id == 0 && $city_id == 0 && $ward_id ==0){
			$data['status'] = 0;
			$data['info'] = '参数不正确';
			$this->ajaxReturn($data);
		}
	
		$obj = D('Admin/ServiceApi');
		$list = $obj->getAreaSalePointList($province_id,$city_id,$ward_id,'sale_point_id,sale_point_name');//查询条件下的数据
		$activity_list = $obj->getActivitySalePoint($activity_id);//活动下的数据
		$res = array();//返回数据初始化
		if(!empty($activity_list)){
			$all_array = array();$activity_array = array();
			$all_array = array_column($list, 'sale_point_id');//全部销售网点ID
			$activity_array = array_column($activity_list, 'sale_point_id');//活动下销售网点ID
			$diff = array_diff($all_array, $activity_array);//数组差
			for($i=0;$i<count($list);$i++){
				if(in_array($list[$i]['sale_point_id'],$diff)){
					//在数组中
					$res[] = $list[$i];
				}
			}
		}else{
			$res = $list;
		}
		$sale_point_ids = array_column($res, 'sale_point_id');//销售网点ID数组
		if(empty($res)){
			$data['status'] = 0;
			$data['info'] = '没有数据';
			$this->ajaxReturn($data);
		}else{
			$data['status'] = 1;
			$data['data'] = $res;
			$data['primary_key'] = 'sale_point_id';
			$data['primary_value'] = $sale_point_ids;
			$data['key'] = array('sale_point_id','sale_point_name');
			$data['key_name'] = array('销售网点序号','销售网点名称');
			$this->ajaxReturn($data);
		}
	}
	
	/**
	 * @todo 根据省，市，区读取除活动已选班车外所有班车列表（公共方法）不受权限控制-----------(待改)
	 * @author sap
	 */
	public function getBusList(){
		//获取数据及判断
		$province_id = (int)I('province');
		$city_id = (int)I('city');
		$ward_id = (int)I('ward');
		$activity_id = (int)I('activity_id');
		$data = array();
		if($activity_id == 0){
			$data['status'] = 0;
			$data['info'] = '参数不正确';
			$this->ajaxReturn($data);
		}
		if($province_id == 0 && $city_id == 0 && $ward_id ==0){
			$data['status'] = 0;
			$data['info'] = '参数不正确';
			$this->ajaxReturn($data);
		}

		$obj = D('Admin/ServiceApi');
		$list = $obj->getAreaBusList($province_id,$city_id,$ward_id,'bus_point_id,bus_point_name');//查询条件下的数据
		$activity_list = $obj->getActivityBusList($activity_id);//活动下的数据
		
		$res = array();//返回数据初始化
		if(!empty($activity_list)){
			$all_array = array();$activity_array = array();
			$all_array = array_column($list, 'bus_point_id');//ID
			$activity_array = array_column($activity_list, 'bus_point_id');//活动下ID
			$diff = array_diff($all_array, $activity_array);//数组差
			for($i=0;$i<count($list);$i++){
				if(in_array($list[$i]['bus_point_id'],$diff)){
					//在数组中
					$res[] = $list[$i];
				}
			}
		}else{
			$res = $list;
		}
		$bus_point_ids = array_column($res, 'bus_point_id');//ID数组
		if(empty($res)){
			$data['status'] = 0;
			$data['info'] = '没有数据';
			$this->ajaxReturn($data);
		}else{
			$data['status'] = 1;
			$data['data'] = $res;
			$data['primary_key'] = 'bus_point_id';
			$data['primary_value'] = $bus_point_ids;
			$data['key'] = array('bus_point_id','bus_point_name');
			$data['key_name'] = array('班车点序号','班车点名称');
			$this->ajaxReturn($data);
		}
	}
	
	
	/**
	 * @todo   Vip票查看及拒收
	 */
	public function vipList(){

	    $activity_id = (int)I('activity_id', 0);
        $mail_status = (int)I('mail_status', -2);
        $phone_number = strip_tags(I('phone_number', ''));
        
        $map = array();
        if($activity_id != 0){
            $map['activity_id'] = $activity_id;
        }else if($activity_id == 0 && $mail_status != -2){
            $this->error('请选择活动');
        }
        if($mail_status != -1 && $mail_status != -2){
            $map['mail_status'] = $mail_status;
        }
        if($phone_number){
            if(!preg_match("/^1[3-8][0-9]{9}$/", $phone_number)){
                $this->error('手机号错误');
            }
            $map['receiving_phone_number'] = $phone_number;
        }
        
        $list = ($map)? $this->lists(M('service_express_detail'),$map,'mail_time asc') : null ;

        $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
        $this->meta_title = 'Vip票查看及拒收';
        $this->assign('activity_list',$activity_list);
        $this->assign('_list', $list);
	    $this->display('vip_list');
	    
	}
	
	/**
	 * @todo   Vip报名用户详细
	 */
	public function vipInfo(){
	    
	    $customer_id = (int)I('customer_id');
	    $activity_id = (int)I('activity_id');
	    if($customer_id == 0 || $activity_id == 0){
	        $this->error('访问参数错误');
	    }
	    $obj_manage = D('ServiceManage');
	    $customer = $obj_manage->getServiceCustomerInfo($customer_id);
	    $activity = $obj_manage->getServiceActivityInfo($customer_id, $activity_id);

	    $this->meta_title = '查看Vip客户信息';
	    $this->assign('customer', $customer);
	    $this->assign('activity', $activity);
	    $this->display('vip_info');
	    
	}
	
	
	/**
	 * @todo   Vip拒收
	 * @param  int     id
	 */
	public function vipStatus($method = null){
	    if ( empty($_REQUEST['id']) ) {
	        $this->error('请选择要操作的数据!');
	    }
	    $id = array_unique((array)I('id',0));
	    $id = is_array($id) ? implode(',',$id) : $id;
	    if ( empty($id) ) {
	        $this->error('请选择要操作的数据!');
	    }

	    $obj_manage = D('ServiceManage');
	    switch ( strtolower($method) ){
	        case 'refuse':
	            //拒收
	            $result = $obj_manage->vipRefuse($id, 3);//3:拒收
	            if($result){
	            	//记录行为
	            	action_log('refuse', 'ServiceManage', $id, UID);
	                $this->success('已拒收');
	            }else{
	                $this->error($obj_manage->getError());
	            }
	            break;
            case 'mail':
                //邮寄
                $result = $obj_manage->vipRefuse($id, 1);//1:邮寄
                if($result){
                	//记录行为
                	action_log('mail', 'ServiceManage', $id, UID);
                    $this->success('已邮寄');
                }else{
                    $this->error($obj_manage->getError());
                }
                break;
	        default:
	            $this->error($method.'参数非法');
	    }
	}

	/**
	 * @todo 查看所有人的推广
	 * @author mahuan <mahuan1768@126.com>
	 */
	public function promoteCustomer(){
	    $this->assign('meta_title','推广客户管理');
	    //获取活动列表
	    $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
	    if(false === $activity_list){
	    	$this->error('获取活动列表出错');
	    }
	    //获取推广部门人员
	    $member_list = D('ServiceApi')->getDepartmentMember(1);
	    if(false === $member_list){
	    	$this->error('获取员工分组出错');
	    }
        $this->assign('member_list',$member_list);
	    $this->assign('activity_list',$activity_list);
	    //搜索条件
	    $map = array();
	    //推广人id
	    $member_id = (int)I('member_id');
	    if($member_id != 0){
	    	$map['generalize_member_id'] = $member_id;
	    }
	    //活动id
	    $activity_id = (int)I('activity_id');
	    if($activity_id != 0){
	    	$map['activity_id'] = $activity_id;
	    }
	    //电话
	    $phone = trim(I('phone'));
	    if($phone){
	    	if (!preg_match("/^1[3-8][0-9]{9}$/",$phone)) {
	    		$this->error('电话号码输入错误。');
	   		}
	   		$customer = D('Admin/Customer','Service')->getCustomerIdByMobilePhone($phone);
	    	if($customer['status'] == 1){
	        	$map['customer_id'] = (int)$customer['data']['customer_id'];
	        }else{
	        	$this->error('没有找到用户！');
	        }
	    }
	    //三者之中必有一项
	    if($member_id == 0 && $activity_id == 0 && empty($phone)){
	    	$this->assign('_list',null);
	    	$this->assign('info_txt','请输入搜索条件！');
	    	$this->display();
	    	return false;
	    }
	    //只显示正常的
	    $map['status'] = 1;
	    $map['is_valid'] = 0;
		$list = $this->lists('ServiceActivityJoin',$map,'customer_id','customer_id,generalize_member_id,activity_id');
		if(false === $list){
			$this->error('查找用户错误。');
		}
		$this->assign('_list',$list);
	    $this->display();
	}
	
	
	/**
	 * @todo 查看我的推广列表
	 * @author sap
	 * @since 2016-2-29
	 */
	public function myPromoteCustomer(){
	    $activity_id = (int)I('activity_id');
	    $phone = trim(strip_tags(I('phone')));
	    
	    //获取活动列表
	    $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
	    if(false === $activity_list){
	        $this->error('获取活动列表出错');
	    }
	    $this->assign('activity_list',$activity_list);
	    
	    //搜索条件
	    $map = array();
	    //活动id
	    if($activity_id != 0){
	        $map['activity_id'] = $activity_id;
	    }
	    //电话
	    if($phone){
	        $customer = D('Admin/Customer','Service')->getCustomerIdByMobilePhone($phone);
	        if($customer['status'] == 1){
	            $map['customer_id'] = (int)$customer['data']['customer_id'];
	        }
	    }
	    $list = null;
        if(!empty($map)){
            if($phone && $customer['status'] != 1){
                $list = null;
            }else{
                $map['generalize_member_id'] = UID;//推广人id
                $map['is_valid'] = 0;//有效
                $list = $this->lists('ServiceActivityJoin',$map,'customer_id','customer_id,generalize_member_id,activity_id,ticket_level');
            }
        }
	    $this->assign('_list',$list);
	    $this->assign('meta_title','我的推广客户');
	    $this->display('my_promote_customer');
	}
	
	/**
	 * @todo   报名转化日志
	 */
	public function changeLogs(){
	    $activity_id = (int)I('activity_id');
	    $member_id = (int)I('member_id');
	    $phone = I('phone');
	    $map = array();
	    //活动id
	    if($activity_id != 0){
	        $map['activity_id'] = $activity_id;
	    }
	    if($member_id != 0){
	        $map['change_member_id'] = $member_id;
	    }
	    if($phone){
	        $customer = D('Admin/Customer','Service')->getCustomerIdByMobilePhone($phone);
	        if($customer['status'] == 1){
	            $map['customer_id'] = (int)$customer['data']['customer_id'];
	        }else{
	            $map['customer_id'] = 0;
	        }
	    }
	    $list = ($map)? $this->lists(D('ServiceChangeLogView'),$map,'log_id desc') : null ;
	    // 获取活动列表
	    $activity_list = D('ServiceApi')->getServiceActivity('1,2,3,4');
	    // 获取转化人列表
	    $member_list = D('ServiceApi')->getDepartmentMember(1);
	    $this->assign('activity_list',$activity_list);
        $this->assign('member_list',$member_list);
	    $this->assign('_list', $list);
	    $this->assign('meta_title','转化日志');
	    $this->display('change_logs');
	}
}
